{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "475b5e7a-f55e-4e0d-ad71-73edc95cbccd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     atan(√x) \n",
      "x ↦ ──────────\n",
      "    √x⋅(x + 1)\n"
     ]
    }
   ],
   "source": [
    "from sympy import *\n",
    "x = symbols('x')\n",
    "\n",
    "# https://zhuanlan.zhihu.com/p/412030129\n",
    "\n",
    "# 不定积分\n",
    "f=Lambda(x, atan(sqrt(x))/((1+x)*sqrt(x)))\n",
    "pprint(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "da11a63f-f0a6-487d-802e-b7018a3f82b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\operatorname{atan}^{2}{\\left(\\sqrt{x} \\right)}$"
      ],
      "text/plain": [
       "atan(sqrt(x))**2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integrate(f(x), x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0256fa5c-d1e5-4000-a3f8-4ab556535b85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{1}{2}$"
      ],
      "text/plain": [
       "1/2"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 定积分\n",
    "\n",
    "integrate(exp(-x), (x, 0, log(2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "991286c4-2aee-4028-be2d-4f5e8f335c8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.97391, -0.86506, -0.67941, -0.43340, -0.14887, 0.14887, 0.43340, 0.67941, 0.86506, 0.97391] [0.066671, 0.14945, 0.21909, 0.26927, 0.29552, 0.29552, 0.26927, 0.21909, 0.14945, 0.066671]\n"
     ]
    }
   ],
   "source": [
    "# 使用高斯-勒让德公式公式来求待积分函数在给定区间上的定积分\n",
    "from sympy.integrals.quadrature import gauss_legendre\n",
    "n_point=10\n",
    "xi, wi = gauss_legendre(n_point, 5)\n",
    "print(xi, wi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "75330834-4fde-47ac-81bf-ec1ac1f3433d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 7.8647$"
      ],
      "text/plain": [
       "7.8647"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f=Lambda(x, sqrt(16+6*x-x**2))\n",
    "gauss_sum=0\n",
    "for i in range(0, n_point):\n",
    "    gauss_sum=wi[i]*f(xi[i])+gauss_sum\n",
    "gauss_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d86c46fe-2699-4b48-b12d-c36c73dcdf3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{\\sqrt{\\pi}}{2}$"
      ],
      "text/plain": [
       "sqrt(pi)/2"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 广义积分\n",
    "\n",
    "# 无穷积分\n",
    "integrate(exp(-x**2), (x, 0, oo))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5b2c4292-e7b3-4dd5-919c-98ec109708c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\infty$"
      ],
      "text/plain": [
       "oo"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 瑕积分\n",
    "integrate(1/(1-x)**2, (x, 0, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6af73c15-cf5f-4427-9492-be87bc27b29d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
